﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;
using Utility;

namespace StudentAppointmentPlatform.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(Session["UserName"] as string))
            {
                Alert.AlertOnPage(Page, Session["UserName"].ToString() + "，您已经登录，将为您跳转到默认页面。");
                //Response.Redirect("/Default.aspx"); //使用这种方式跳转则直接跳转了，上面的弹出提示将不会显示;
                Redirect.Js_Redirect(Page, "/Default.aspx");
            }

            if (!IsPostBack)
            {
                #region 设置用户编号

                string yearTail = DateTime.Now.Year.ToString().Substring(2);
                string latestUserId = UserBO.GetLatestUserIdByYearTail(yearTail);
                if (!string.IsNullOrEmpty(latestUserId))
                {
                    UserId.Text = (int.Parse(latestUserId) + 1).ToString();
                }
                else
                {
                    UserId.Text = yearTail + "0001";
                }

                #endregion
            }
        }

        protected void CreateUserButton_OnClick(object sender, EventArgs e)
        {
            User user = new User();

            user.UserId = UserId.Text;
            user.UserName = UserName.Text;
            user.Password = Password.Text;
            user.Email = Email.Text;
            user.Telephone = Telephone.Text;
            user.Cellphone = Cellphone.Text;
            if (RB_Stu.Checked)
            {
                user.Type = "Student";
            }
            else
            { //RB_Dir..Checked
                user.Type = "Director";
            }

            if (UserBO.InsertUser(user) > 0)
            {
                Alert.AlertOnPage(Page, "创建用户成功，将为您跳转到登录页面。");
                Redirect.Js_Redirect(Page, "/Account/Register.aspx");
            }
            else
            {
                Alert.AlertOnPage(Page, "创建用户失败。");
            }
        }
    }
}
